/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quan;

import java.awt.Dimension;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

/**
 *
 * @author Quannk
 */
public class QLayout {

    Point startPoint = new Point(20, 20);

    private void horizontalLayout() {
        int x, y = startPoint.y;
        int desktopPaneWidth = desktopPane.getPreferredSize().width;
        int totalWidth = 0;
        boolean isBigger = false;
        for (Integer key : map.keySet()) {
            ArrayList<QInternalFrame> frames = map.get(key);
            totalWidth = getTotalWidth(frames);
            int temp = (desktopPaneWidth - totalWidth) / 2;
            while (temp < 0) {
                isBigger = true;
                int newWidth = totalWidth + 100;
                int newHeight = (newWidth * desktopPane.getHeight()) / desktopPane.getWidth();
                desktopPane.setPreferredSize(new Dimension(newWidth, newHeight));
                desktopPane.setSize(newWidth, newHeight);
                desktopPaneWidth = desktopPane.getPreferredSize().width;
                temp = (desktopPaneWidth - totalWidth) / 2;
            }
            for (int i = 0; i < frames.size(); i++) {
                x = temp + frames.get(i).getWidth() * 2 * i;
                frames.get(i).setLocation(x, y);
            }
            if (frames.size() > 0) {
                y = frames.get(0).getHeight() * 2 * key;
            }
        }
        if (isBigger) {
            horizontalLayout();
        }
    }

    private void verticalLayout() {
        int x = startPoint.x, y;
        int desktopPaneHeight = desktopPane.getPreferredSize().height;
        int totalHeight = 0;
        boolean isBigger = false;
        for (Integer key : map.keySet()) {
            ArrayList<QInternalFrame> frames = map.get(key);
            totalHeight = getTotalHeight(frames);
            int temp = (desktopPaneHeight - totalHeight) / 2;
            while (temp < 0) {
                isBigger = true;
                int newHeight = totalHeight + 200;
                int newWidth = (newHeight * desktopPane.getWidth()) / desktopPane.getHeight();
                desktopPane.setPreferredSize(new Dimension(newWidth, newHeight));
                desktopPane.setSize(newWidth, newHeight);
                desktopPaneHeight = desktopPane.getPreferredSize().height;
                temp = (desktopPaneHeight - totalHeight) / 2;
            }
            for (int i = 0; i < frames.size(); i++) {
                y = temp + frames.get(i).getHeight() * 2 * i;
                frames.get(i).setLocation(x, y);
            }
            if (frames.size() > 0) {
                x = frames.get(0).getWidth() * 2 * key;
            }
        }
        if (isBigger) {
            verticalLayout();
        }
    }

    private int getTotalHeight(ArrayList<QInternalFrame> frames) {
        int result = 0;
        if (frames.size() > 0) {
            result = frames.get(0).getHeight() * (frames.size() * 2 - 1);
        }
        return result;
    }

    public enum Orientation {

        Horizontal,
        Vertical
    }
    private QDesktopPane desktopPane;
    HashMap<Integer, ArrayList<QInternalFrame>> map;

    private int getMaxHeight() {
        int result = 0;
        JInternalFrame[] frames = desktopPane.getAllFrames();
        if (frames.length == 0) {
            return result;
        }
        result = frames[0].getHeight();
        return result;
    }

    private int getMaxLevel() {
        int result = 0;
        JInternalFrame[] frames = desktopPane.getAllFrames();
        for (int i = 0; i < frames.length; i++) {
            QInternalFrame qFrame = (QInternalFrame) frames[i];
            if (qFrame.level > result) {
                result = qFrame.level;
            }
        }
        return result;
    }

    private void initMap() {
        map = new HashMap<>();
        int maxLevel = getMaxLevel();
        ArrayList<JInternalFrame> all = new ArrayList<>();
        all.addAll(Arrays.asList(desktopPane.getAllFrames()));

        for (int i = 1; i <= maxLevel; i++) {
            ArrayList<QInternalFrame> frames = new ArrayList<>();
            int index = 0;
            for (int j = 0; j < all.size(); j++) {
                QInternalFrame qFrame = (QInternalFrame) all.get(j);
                if (qFrame.level == i) {
                    qFrame.index = index;
                    index++;
                    frames.add(qFrame);
                }
            }
            Collections.sort(frames, new QComparator());
            map.put(i, frames);
            all.removeAll(frames);
        }

    }

    public QLayout(QDesktopPane desktopPane1) {
        this.desktopPane = desktopPane1;
        initMap();
    }

    public void performLayout(Orientation orientation) {
        if (orientation == Orientation.Horizontal) {
            horizontalLayout();
        } else {
            verticalLayout();
        }
    }

    private int getTotalWidth(ArrayList<QInternalFrame> frames) {
        int result = 0;
        if (frames.size() > 0) {
            result = frames.get(0).getWidth() * (frames.size() * 2 - 1);
        }
        return result;
    }
}
